#include"../include/lib_headers.h"
char* move_file(char *source, char *destination) {
	int i = 0, j = 0, k = 0, match = 0, t = 0;
	char *s, input[SIZE] = "", *split_char, *p[SIZE], copy[SIZE] = "", dest[SIZE] = "",*s1, var[SIZE]="" , parent[SIZE] = "" ;
	node *temp = NULL , *prev = NULL , *current = NULL , *source1 = NULL , *destination1 = NULL , *child = NULL ;

	/* COPYING THE PARENT INTO CHARACTER ARRAY */
	strcpy ( parent , getparent( destination ) ) ;

	/* VFS not mounted */
	if (h == NULL || l1 == NULL) {
		sprintf(output_message,"%s %s","movefile_FAILURE",ERR_VFS_MOVEFILE_06);	
		return output_message;
	}

	/* Insufficient Arguments */	
	if(strlen(source) == 0|| strlen(destination) == 0){
		sprintf(output_message,"%s %s","movefile_FAILURE",ERR_VFS_MOVEFILE_00);	
		return output_message;
	}

	/* Both Source and Destination are same, so no point of moving it */
	if(strcmp(source, destination) == 0){
		sprintf(output_message,"%s %s","movefile_FAILURE",ERR_VFS_MOVEFILE_00);	
		return output_message;
	}

	/* Cannot find source file */
	if(find_node(l1->next,source)==NULL || source[0] != '/' ){
		sprintf(output_message,"%s %s","movefile_FAILURE",ERR_VFS_MOVEFILE_01);	
		return output_message;
	}
	
	/* IF NO DOTS ARE THERE THEN IT'S CLEAR THAT SOURCE FILE PATH DOES NOT EXIST */
	if ( strchr ( source , '.' ) == NULL ){
		sprintf(output_message,"%s %s","movefile_FAILURE",ERR_VFS_MOVEFILE_01);	
		return output_message;
	} 

	/* CHECKING IF DESTINATION HAS A FILE PATH */
	if ( strchr ( destination , '.' ) == NULL || destination[0] != '/' ){
		sprintf(output_message,"%s %s","movefile_FAILURE",ERR_VFS_MOVEFILE_02);	
		return output_message;
	}
	

	/* Extensions are not same */	
	if(strcmp(file_extension(source), file_extension(destination)) != 0){
		sprintf(output_message,"%s %s","movefile_FAILURE",ERR_VFS_MOVEFILE_02);	
		return output_message;
	}
	
	/* Destination directory does not exist where file has to be moved*/
	if(find_node(l1->next, parent )==NULL){
		sprintf(output_message,"%s %s","movefile_FAILURE",ERR_VFS_MOVEFILE_02);	
		return output_message;
	}
	
	/* If the destination is just the root and no file path */
	if(strcmp(destination, "/") == 0){
		sprintf(output_message,"%s %s","movefile_FAILURE",ERR_VFS_MOVEFILE_01);	
		return output_message;
	}
	
	/* Cannot move a file to a directory */
	if(find_node(l1->next,destination) != NULL && strcmp(find_node(l1->next,destination)->fd_pointer->fileType, "dir") == 0){
		sprintf(output_message,"%s %s","movefile_FAILURE",ERR_VFS_MOVEFILE_02);	
		return output_message;
	}	
	
	source1=find_node(l1->next, source); destination1=find_node(l1->next, parent );

	if(strcmp(destination, "/") != 0)	
		child=destination1->child;
	else
		child = destination1;	
	

	/*Source file is already there in the destination */
	if(find_node(l1->next,destination)!=NULL){
		remove_file(destination);
	}



	del(source1->fd_pointer->fullPath);
	htable_delete(hashtable,*(source1->fd_pointer));

	split_char = "/";
	s = add_slash(source);

	strcpy(input, s);

	i = splits_directory(input, p, split_char);
	char deleted_text[SIZE] = "";
	node *deleted;
	temp = l1->next;
	prev = temp;
	match = 0;
	j = 0;

	while (j < i) {
		while (temp != NULL) {
			if (strcmp(temp->fd_pointer->fileName, p[j]) == 0) {
				match = 1;
				j++;
				if (j == i) {
					break;
				}
				prev = temp;
				temp = temp->child;
			}
			if (match == 0) {
				prev = temp;
				temp = temp->sibling;
			} else if (match == 1) {
				match = 0;
				continue;
			}
		}
	}

	if (prev->child == temp) {
		strcpy(deleted_text, prev->child->fd_pointer->fileName);
		deleted = prev->child;
		prev->child = temp->sibling;
		del(temp->fd_pointer->fullPath);
		htable_delete(hashtable,*(temp->fd_pointer));
	} else if (prev->sibling == temp) {
		strcpy(deleted_text, prev->sibling->fd_pointer->fileName);
		deleted = prev->sibling;
		prev->sibling = prev->sibling->sibling;
		del(temp->fd_pointer->fullPath);
		htable_delete(hashtable,*(temp->fd_pointer));
	} else if (prev == l1->next) {
		strcpy(deleted_text, prev->fd_pointer->fileName);
		deleted = prev;
		temp = deleted;
		l1->next = prev->sibling;
		del(prev->fd_pointer->fullPath);
		htable_delete(hashtable,*(prev->fd_pointer));
	}
		
		deleted->sibling = NULL;		
		s1 = add_slash(destination);
		strcpy(dest, s1);
		current = temp;		
		strcpy(var, parent );
		
		match = 0;
		j = 0;
		temp = l1->next;
		prev = temp;
		split_char = "/";
		i = splits_directory(dest, p, split_char);
		/* FRAGMENTING THE ENTIRE SO THAT WE GET THE FILE PATH */
		strcpy(current->fd_pointer->fileName, p[i-1]);	
		/* THIS TIME ONLY FRAGMENTING THE PARENT */
		i = splits_directory(parent, p, split_char);
		strcat(var, current->fd_pointer->fileName);
		strcpy(current->fd_pointer->fullPath, var);		
		
		while (j < i) {
			while (temp != NULL) {
				if (strcmp(temp->fd_pointer->fileName, p[j]) == 0) {
					match = 1;
					j++;
					if (j == i) {
						break;
					}
					prev = temp;
					temp = temp->child;
				}
				if (match == 0) {
					prev = temp;
					temp = temp->sibling;
				} else if (match == 1) {
					match = 0;
					continue;
				}
			}
		}
		if(i == 0){
			l1->next = current;
			current->sibling = temp;
		}	
		else  {
			if ( temp->child == NULL ) {
				temp->child = current;
			}	
			else {
				node *hold = temp->child;
				while ( hold->sibling != NULL )
					hold = hold->sibling;
				hold->sibling = current;
			}
		}

	insert(current->fd_pointer);
	htable_insert(hashtable,*(current->fd_pointer));
	sprintf(output_message,"%s","movefile_SUCCESS");	
	return output_message;
}
